package 算法回顾;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-17
 **/

public class No3无重复字符的最长子串 {

    public int lengthOfLongestSubstring(String s) {

        int left = 0;
        int right = 0;
        char[] charArray = s.toCharArray();
        int[] arr = new int[256];
        int count = 0;
        int result = 0;

        while (right < s.length()) {
            if (arr[charArray[right]] != 0 || count !=0) {
                // 滑动窗口开始移动了
                arr[charArray[left]]--;
                if (arr[charArray[left]] == 1) {
                    count--;
                }
                left++;
            }
            arr[charArray[right]]++;
            if (arr[charArray[right]] == 2) {
                // 计数
                count++;
            }
            right++;
            result = Math.max(result, right - left);
        }

        return result;
    }

    public static void main(String[] args) {
        No3无重复字符的最长子串 n = new No3无重复字符的最长子串();
        String str = " ";
        int result = n.lengthOfLongestSubstring(str);
        System.out.println(result);
    }

}
